#################################################################################
# Filename: 4_round2_mediation.R
# Description: Takes the cleaned round 2 data and runs
# mediation analysis displayed in Appendix D
#################################################################################

library(mediation)

# Import cleaned data from 1_round2_loaddata
dat <- import("Round_2_clean.Rdata") 




### Do the mediation analysis 
#Remove NAs for outcome, mediator
data <- dat[is.na(dat$antiforeign_japan_pca)==FALSE,]
data <- data[is.na(data$Q19_1)==FALSE,]
data <- data[which(data$clip!="HK"),]


#Linear regressions of treatment on mediator, mediator and treatment on outcome
med.fit <- lm(Q19_1~clip, data=data)
out.fit <- lm(antiforeign_japan_pca~Q19_1+ clip, data=data)
med.out <- mediate(med.fit, out.fit, treat = "clip", mediator = "Q19_1", robustSE = TRUE, sims = 5000)

summary(med.out)


pdf("r2_mediation_drama.pdf")
par(mar=c(7,8,1, 1) )
plot(med.out, xlab="Round 2: Anti-Japanese War Drama", yaxt = "n", cex=2, cex.lab=1.5, col = "red", xlim=c(-0.15, 0.5))
axis(2, at=1:3, labels=c("Total\nEffect", "Direct\nEffect", "Effect\nMediated\nby Anger"), las=2, cex.axis=1.5)
dev.off()




#Remove NAs for outcome, mediator
data <- dat[is.na(dat$antiforeign_usa_pca)==FALSE,]
data <- data[is.na(data$Q19_1)==FALSE,]
data <- data[which(data$clip!="Japan"),]


#Linear regressions of treatment on mediator, mediator and treatment on outcome
med.fit <- lm(Q19_1~clip, data=data)
out.fit <- lm(antiforeign_usa_pca~Q19_1+ clip, data=data)
med.out <- mediate(med.fit, out.fit, treat = "clip", mediator = "Q19_1", robustSE = TRUE, sims = 5000)

#Results
summary(med.out)


pdf("r2_mediation_news.pdf")
par(mar=c(7,8,1, 1) )
plot(med.out, xlab="Round 2: Anti-US social media", yaxt = "n", cex=2, cex.lab=1.5, col = "red", xlim=c(-0.15, 0.5))
axis(2, at=1:3, labels=c("Total\nEffect", "Direct\nEffect", "Effect\nMediated\nby Anger"), las=2, cex.axis=1.5)
dev.off()



#Remove NAs for outcome, mediator
data <- dat[is.na(dat$antiforeign_japan_pca)==FALSE,]
data <- data[is.na(data$Q19_2)==FALSE,]
data <- data[which(data$clip!="HK"),]


#Linear regressions of treatment on mediator, mediator and treatment on outcome
med.fit <- lm(Q19_2~clip, data=data)
out.fit <- lm(antiforeign_japan_pca~Q19_2+ clip, data=data)
med.out <- mediate(med.fit, out.fit, treat = "clip", mediator = "Q19_2", robustSE = TRUE, sims = 5000)

summary(med.out)


pdf("r2_mediation_drama_fear.pdf")
par(mar=c(7,8,1, 1) )
plot(med.out, xlab="Round 2: Anti-Japanese War Drama", yaxt = "n", cex=2, cex.lab=1.5, col = "red", xlim=c(-0.15, 0.5))
axis(2, at=1:3, labels=c("Total\nEffect", "Direct\nEffect", "Effect\nMediated\nby Fear"), las=2, cex.axis=1.5)
dev.off()




#Remove NAs for outcome, mediator
data <- dat[is.na(dat$antiforeign_usa_pca)==FALSE,]
data <- data[is.na(data$Q19_2)==FALSE,]
data <- data[which(data$clip!="Japan"),]


#Linear regressions of treatment on mediator, mediator and treatment on outcome
med.fit <- lm(Q19_2~clip, data=data)
out.fit <- lm(antiforeign_usa_pca~Q19_2+ clip, data=data)
med.out <- mediate(med.fit, out.fit, treat = "clip", mediator = "Q19_2", robustSE = TRUE, sims = 5000)

#Results
summary(med.out)


pdf("r2_mediation_news_fear.pdf")
par(mar=c(7,8,1, 1) )
plot(med.out, xlab="Round 2: Anti-US social media", yaxt = "n", cex=2, cex.lab=1.5, col = "red", xlim=c(-0.15, 0.5))
axis(2, at=1:3, labels=c("Total\nEffect", "Direct\nEffect", "Effect\nMediated\nby Fear"), las=2, cex.axis=1.5)
dev.off()